import numpy as np
import csv
import random
from datetime import datetime
from time import strftime
from scipy.stats import scoreatpercentile

'''
revision of test24
to calculate the false nagetive, false positive

'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate


def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
        if y == '9091' or y == '9192':
            sKey.append(-9)
    sKey = np.array(sKey)
    sKey.shape=(-1,8)
    return sKey

def build_data(inputFile):
    sKey = []
    reader = csv.reader(open(inputFile, 'rb'), delimiter=',')
    i = 0
    for record in reader:
        for item in record:
            sKey.append(item)
        i += 1
    sKey = np.array(sKey)
    sKey.shape=(i, -1)
    return sKey

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    #year = ['9091', '9192', '9293', '9394', '9495', '9596', '9697', '9798', '9899', '9900', '0001', '0102', '0203', '0304', '0405', '0506', '0607', '0708']
    year = ['9293', '9394', '9495', '9596', '9697', '9798', '9899', '9900', '0001', '0102', '0203', '0304', '0405', '0506', '0607', '0708']
    print len(year)
    filePath = 'C:/_DATA/migration89_08/COUNTY Migration/csv/u/'
    output = np.array([])
    for y in year:
        print y
        inputCSV = filePath + y + 'o.csv'
        data = build_data(inputCSV)
        output = np.append(output, data)
    output.shape = (-1, 10)
    #print output
    fileLoc = filePath + 'total_out.csv'
    np.savetxt(fileLoc, output, delimiter=',', fmt = '%s')
    print len(output)

    print "end at " + getCurTime()
    print "========================================================================"  

           
